package com.legal.aid.app.controller;

import com.legal.aid.app.domain.doc.NewsDoc;
import com.legal.aid.app.domain.vo.NewsVo;
import com.legal.aid.app.service.IAppNewsService;
import com.legal.aid.common.core.controller.BaseController;
import com.legal.aid.common.core.domain.AjaxResult;
import com.legal.aid.common.core.page.TableDataInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/app/news")
public class AppNewController extends BaseController {

    @Autowired
    private IAppNewsService appNewsService;

    /**
     * 新闻列表 可以根据分类查询 newsCategory 字典值
     * 
     * @param newsCategory
     * @return
     */
    @GetMapping("/list")
    public TableDataInfo list(@RequestParam(value = "newsCategory", required = false) Integer newsCategory) {
        startPage();
        NewsVo newsVo = new NewsVo();
        if (newsCategory != null) {
            newsVo.setNewsCategory(String.valueOf(newsCategory));
        }
        List<NewsVo> list = appNewsService.selectNewsList(newsVo);
        return getDataTable(list);
    }

    /**
     * 查询新闻
     */
    @GetMapping("/search")
    public TableDataInfo searchNews(@RequestParam("keyword") String keyword,
            @RequestParam(value = "pageNum", defaultValue = "1") int pageNum,
            @RequestParam(value = "pageSize", defaultValue = "10") int pageSize) {
        startPage();
        List<NewsDoc> list = appNewsService.searchNewsByKeyword(keyword, pageNum, pageSize);
        return getDataTable(list);
    }

    /**
     * 返回补全内容
     * 
     * @param prefix
     * @return
     */
    @GetMapping("/suggestion")
    public AjaxResult getSuggestions(@RequestParam("key") String prefix) {
        List<String> list = appNewsService.getSuggestions(prefix);
        return success(list);
    }

    @GetMapping("/getHotKeys")
    public AjaxResult getHotKeys() {
        List<String> list = appNewsService.getNewsHotKeys();
        return success(list);
    }

    /**
     * es
     * @return
     */
    @PutMapping("/updateNewsToEs")
    public AjaxResult update() {
        return appNewsService.updateToEs() > 0 ? AjaxResult.success() : AjaxResult.error("news数据更新到es失败", 502);

    }

    /**
     * 新闻列表 可以根据分类查询 newsCategory 字典值
     *
     * @return
     */
    @GetMapping("/list/top")
    public TableDataInfo listTopList() {
        startPage();
        List<NewsVo> list = appNewsService.selectTopNews();
        return getDataTable(list);
    }

    /**
     * 获取当前登录用户新闻的关注列表
     *
     */
    @GetMapping("/attention")
    public AjaxResult selectAttentionNewsList() {
        List<NewsVo> list = appNewsService.selectAttentionNewsVoList();
        return AjaxResult.success(list);
    }

    /**
     * 新闻详情
     */
    @GetMapping(value = "/{newsId}")
    public AjaxResult selectNewsByNewsId(@PathVariable("newsId") String newsId) {
        return AjaxResult.success(appNewsService.getNewsDetailByNewsId(newsId));
    }
}
